/**
 * 
 */
package com.risetek.tft.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risetek.tft.entity.TOperate;
import com.risetek.tft.entity.TOperateId;
import com.risetek.tft.service.MenuService;
import com.risetek.tft.service.OperateService;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DwzModel;

/**
 * @author hetao
 * 
 */
@Controller
@RequestMapping("/operate")
public class OperateAction extends BaseAction {

	@RequestMapping(value = "/edit")
	public String edit(TOperateId id, ModelMap model) {
		if (id != null) {
			TOperate dto = operateService.findById(id);
			model.addAttribute("dto", dto);
		}
		return "operate/edit";
	}
	
	@RequestMapping(value = "/add")
	public String add(  ModelMap model) {
		TOperate dto = new TOperate();
			model.addAttribute("dto", dto);
		return "operate/edit";
	}
	 
	
	@RequestMapping(value="/list" )
	@ResponseBody
	public DwzModel listview(ModelMap model, DwzModel dataGrid ,String name ,HttpServletRequest request)
		  {//type=provider  
		    StringBuffer hql = new StringBuffer("select new map(a.id.menu as menu,a.id.op as op,a.TMenu.name as menuname,a.name as name,a.icon as icon,a.ordno as ordno,a.isshow as isshow) FROM TOperate a ");
		    if(StringUtils.isNotEmpty(name)){
		    	hql.append(" and a.account = '"+name+"' ");
		    }
			 if(StringUtils.isNotEmpty(dataGrid.getOrderField())){
				hql.append( "order by a."+dataGrid.getOrderField()+"  "+dataGrid.getOrderDirection());
			}else{
				hql.append("order by  a.id ");
			} 
	        dataGrid = this.operateService.pagedQuery(hql.toString(),"select count(*) from TOperate t      ",   dataGrid);
	       
           return dataGrid;
	}
	
	
	

	@RequestMapping(value = "/save")
	@ResponseBody
	public AjaxJson save(@ModelAttribute(value = "dto") TOperate dto,
			BindingResult bindingResult, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		AjaxJson json = new AjaxJson();
		try {
			if (dto.getId().getMenu() == null && StringUtils.isEmpty(dto.getId().getOp())) {
				// farmOperateService.save(dto);
			} else {
				TOperate persistent = operateService.findById(dto.getId());
				if (persistent == null) {
					persistent = new TOperate();
					persistent.setId(dto.getId());
				}
				persistent.setTMenu(menuService.findById(dto.getId().getMenu()));
				persistent.setName(dto.getName());
				persistent.setIcon(dto.getIcon());
				persistent.setIsshow(dto.getIsshow());
				persistent.setOrdno(dto.getOrdno());
				persistent.setRemark(dto.getRemark());
				operateService.saveOrUpdate(persistent);
			}
			json.setSuccess(true);
			json.setMsg("保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("保存失败！");
		}
		return json;
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public AjaxJson del(String[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson json = new AjaxJson();
		try {
			List<Serializable> idList = new ArrayList<Serializable>();
			for(String tid : ids) {
				String[] tids = tid.split("-");
				TOperateId id = new TOperateId(Integer.valueOf(tids[0]), tids[1]);
				idList.add(id);
			}
			operateService.delete(idList.toArray(new Serializable[]{}));
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("删除失败！");
		}
		return json;
	}

	@Autowired
	private OperateService operateService;
	@Autowired
	private MenuService menuService;
	
}
